package cn.ztf.zshop.common.utils;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.LinkedList;
import java.util.List;
import java.util.function.BiFunction;

public class PageConverterVo {


    public static <T, R> Page<R>  converterPage(Page<T> page, Class<R> rClass, BiFunction<T, R, R> function) {

        Page<R> objectPage = Page.of(page.getCurrent(), page.getSize(), page.getTotal());
        LinkedList<R> list = new LinkedList<>();
        objectPage.setRecords(list);

        List<T> records = page.getRecords();
        for (T record : records) {
            R rObject = BeanUtil.toBean(record, rClass);
            if (function != null) {
                rObject = function.apply(record, rObject);
            }
            list.add(rObject);
        }
        return objectPage;
    }


    public static  <T, R> Page<R> converterPage(Page<T> page, Class<R> rClass) {
        return converterPage(page, rClass, null);
    }

}
